<template>
  <div class="listmenu">
    <div class="user_s">
      <el-input v-model="userInput" placeholder="权限名" />

      <el-button type="primary" :icon="Search" @click="suouser()">搜索</el-button>
      <el-button type="info" @click="dialogVisibleclick()">添加</el-button>
    </div>
    <el-table :data="menulist" border style="width: 100%">
      <el-table-column prop="id" label="序号" />
      <el-table-column prop="menuname" label="权限名" />
      <el-table-column prop="menupath" label="路径" />
      <el-table-column prop="menuLevel" label="级别" >
      <template #default="scope">
          <span>{{scope.row.menuLevel==1?'一级':'二级'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            @click="dialogVisibleclick(scope.row)"
            :icon="Edit"
          ></el-button>
          <el-button
            type="primary"
            @click="deteleUser(scope.row.id)"
            :icon="Delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <div class="page">
        <el-pagination
          background
          v-model:currentPage="page.pageNo"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="num"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
    </div> -->

    <el-dialog
      v-model="dialogVisible"
      title="Tips"
      width="30%"

    >
      <el-form
        label-position="Left"
        label-width="100px"
        :model="userEdit"
        style="max-width: 460px"
      >
        <el-form-item label="权限名:">
          <el-input v-model="userEdit.menuname"></el-input>
        </el-form-item>
        <el-form-item label="路径:">
          <el-input v-model="userEdit.menupath"></el-input>
        </el-form-item>
        <el-form-item label="图标:">
          <el-input v-model="userEdit.menuicon"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogVisibleEdite()">确认</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="dialogrole" width="30%">
      <el-select
        v-model="seRole"
        @change="changeRole($event)"
        class="m-2"
        placeholder="请选择"
        size="large"
      >
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.roleName"
          :value="item.id"
        >
        </el-option>
      </el-select>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogrole = false">取消</el-button>
          <el-button type="primary" @click="dialogRoleupadte">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  defineComponent,
  onMounted,
  getCurrentInstance,
  reactive,
  toRefs,
  ref,
  toRaw,
} from "vue";
import { Edit, Share, Delete, Search, Upload, Tools } from "@element-plus/icons-vue";
// import { request } from "@/network/request";
import httpRequest from '../service';

import { ElMessage, ElMessageBox } from "element-plus";
const userInput = ref("");
let tableData = ref([]);
const dialogVisible = ref(false);
const dialogrole = ref(false);
let options = ref([]);
let seRole = ref("");
let roleSe = ref("");
let userIrole = ref("");
let userEdit = ref({});
let userUpdateRole = ref({});
let updateAdd = ref<any>("");
// let num = ref<Number>(0);
const menulist = ref<any[]>([]);
// const page = reactive({
//   pageNo: 1,
//   pageSize: 20,
//   menuname: userInput.value,
// });

onMounted(() => {
  getUsers();
  getRoe();
});

/*
// 分页
// const handleSizeChange = (pageSize: number | string) => {
//   page.pageNo = 1;
//   page.pageSize = Number(pageSize);
//   getlistusers();
// };
// const handleCurrentChange = () => {
//   getlistusers();
// };
*/

const getlistusers = () => {
  httpRequest.request({ url: "/menu/listmenu",method: 'post'}).then((res: any) => {
    // console.log(res.dats.);
    
    let ms = res.data;
    let sList = [];
    let s = {};

    for (let index = 0; index < ms.length; index++) {
      if ("children" in ms[index]) {
        s = {
          id: ms[index].id,
          menuLevel: ms[index].menuLevel,
          menuname: ms[index].menuname,
          menupath: ms[index].menupath,
          menuicon: ms[index].menupath.menuicon,
          isDel: ms[index].isDel,
        };
        ds(ms[index].children, sList);
        sList.push(s);
      }
    }
    menulist.value = sList;
    let {
      data
    } = res;
    tableData.value = data;
  });
};

const ds = (sd:any, sList:any) => {
  let s;
  for (let index = 0; index < sd.length; index++) {
    if ("children" in sd[index]) {
      s = {
        id: sd[index].id,
        menuLevel: sd[index].menuLevel,
        menuname: sd[index].menuname,
        menupath: sd[index].menupath,
        menuicon: sd[index].menupath.menuicon,
        isDel: sd[index].isDel,
        children:[]
      };
      ds(sd[index].children, s.children);
    } else {
      s = {
        id: sd[index].id,
        menuLevel: sd[index].menuLevel,
        menuname: sd[index].menuname,
        menupath: sd[index].menupath,
        menuicon: sd[index].menupath.menuicon,
        isDel: sd[index].isDel,
      };
    }
    sList.push(s);
  }
};

// 搜索
const suouser = () => {
  // page.pageNo = 1;
  getlistusers();
};

// 获取角色
const getRoe = () => {
  httpRequest.request({ url: "/roles/listallroles",method: 'post' }).then((res: any) => {
    options.value = res.data.data;
  });
};
// 删除管理员
const deteleUser = (userId:any) => {
  
  httpRequest.request({ url: "/menu/delmenu", data: { id: userId },method: 'post' }).then((res: any) => {
    ElMessage({
      message: "删除成功",
      type: "success",
    });
    getUsers();
  });
};

// 获取所有管理员
const getUsers = () => {
  getlistusers();
};

// 获取选择的值
const changeRole = (role:any) => {
  roleSe.value = role;
};

// 修改角色(1.1)
const updateUser = (userId:any) => {
  dialogrole.value = true;
  userIrole.value = userId;
};

// 修改角色(1.2)
const dialogRoleupadte = () => {
  dialogrole.value = false;
  httpRequest.request({
    url: "/users/updusers",
    data: { id: userIrole.value, roleid: roleSe.value },
    method: 'post'
  }).then((res: any) => {
    ElMessage({
      message: "修改成功",
      type: "success",
    });
    getUsers();
  });
};

// 管理员状态
const changStatus = (val:any, userId:any) => {
  let user = {
    id: 0,
    chkStatus: "",
  };
  user.id = userId;
  user.chkStatus = val;
  console.log(user);
  httpRequest.request({ url: "/users/updusers", data: user,method: 'post' }).then((res: any) => {
    ElMessage({
      message: "修改成功",
      type: "success",
    });
  });
};

// 编辑(1.1)
const dialogVisibleclick = (userdetail:any) => {
  if (userdetail == undefined) {
    updateAdd.value = 0;
    userEdit.value = {};
  } else {
    userEdit.value = { ...userdetail };
    
  }
  dialogVisible.value = true;
};

//  编辑(1.2)
const dialogVisibleEdite = () => {
  dialogVisible.value = false;
  if (updateAdd._rawValue == 0) {
    httpRequest.request({ url: "/menu/updmenu", data: {...userEdit},method: 'post' }).then(
      (res: any) => {
        ElMessage({
          message: "添加成功",
          type: "success",
        });
        getUsers();
      }
    );
  } else {
    httpRequest.request({ url: "/menu/updmenu", data: {...userEdit},method: 'post' }).then(
      (res: any) => {
        ElMessage({
          message: "修改成功",
          type: "success",
        });
        getUsers();
      }
    );
  }
};
</script>

<style lang="less" scope>

.listmenu{
  width: 100%;
  height: 100%;
}

.user_s {
  display: flex;
  margin-bottom: 10px;
  .el-input {
    width: 240px;
    // margin-left:20px;
    margin-right: 20px;
  }

  .el-button {
    margin-left: 20px;
  }
}
.page {
  margin-top: 30px;
}
</style>
